package org.iabako.client.ui.form.user.userconfiguration;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.config.page.PageConfigSearchEnum;
import org.iabako.client.config.page.PageEnum;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.ui.form.AbstractForm;
import org.iabako.client.ui.layout.field.IField;
import org.iabako.client.ui.layout.field.LabelText;
import org.iabako.client.ui.layout.widget.datatable.DataTable;
import org.iabako.shared.entity.business.AbstractEntity;
import org.iabako.shared.iservice.user.UserServiceAsync;
import org.iabako.shared.querydefinition.dto.QueryDefinitionDTO;

import java.util.List;

/**
 * Created by jose on 24/09/14.
 */
public class UsersForm extends AbstractForm {

    interface MyUiBinder extends UiBinder<HTMLPanel, UsersForm> {
    }

    protected MyUiBinder binderForm = GWT.create(MyUiBinder.class);

    protected final UserServiceAsync userServiceAsync = UserServiceAsync.Util.getInstance();

    @UiField
    HTMLPanel usersResultPanel;
    @UiField
    HTMLPanel usersPanel;
    @UiField
    LabelText addNewUser;
    @UiField
    LabelText buyCreditLink;

    DataTable dataTable;

    public UsersForm() {
        initWidget(binderForm.createAndBindUi(this));
    }

    @Override
    public void initialize() {
        if (dataTable != null) {
            return;
        }

        usersPanel.setVisible(true);

        final UsersForm parentUsersForm = this;

        dataTable = new DataTable(userServiceAsync, userServiceAsync, new QueryDefinitionDTO(), PageEnum.UserModify, PageConfigSearchEnum.UserSearch) {
            @Override
            protected void customTaskBeforeRender() {
                super.customTaskBeforeRender();
                queryDefinitionDTO.setPageSize(-1);
            }

            @Override
            protected void rowSelectionAction(Event event, String rowSelectedId) {
                rowSelectionAction(parentUsersForm, event, rowSelectedId);
            }
        };

        addNewUser.getLabel().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                NavigationController.getInstance().goTo(PageEnum.UserCreate, parentUsersForm);
            }
        });

        buyCreditLink.getLabel().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                NavigationController.getInstance().goTo(PageEnum.BuyCredit);
            }
        });

        dataTable.show(usersResultPanel);
        dataTable.disableExternalActions();

    }

    public void refreshUsers() {
        dataTable.refreshSearch(new QueryDefinitionDTO(), false);
    }

    @Override
    protected List<IField> getMandatoryFields() {
        return null;
    }

    @Override
    protected AbstractEntity getEntity() {
        return null;
    }

    @Override
    public boolean validate() {
        return false;
    }
}
